package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.Util;
import model.Course;
import model.Student;

public class CourDao {
	public List <Course> findAll(){
		List <Course> cours = new ArrayList<Course>();
		Connection con = null;
		try {
			con = Util.getConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from db_course_p");
			while(rs.next()){
				Course cour = new Course(rs.getInt("id"),rs.getString("cname"),rs.getInt("period"));
				cours.add(cour);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return cours;
		}
		finally{
			try {
				Util.closeConnection(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cours;
	}
	public List <Course> findCourseByName(String name){
		List <Course> cours = new ArrayList<Course>();
		Connection con = null;
		try {
			con = Util.getConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from db_course_p where cname like '%"+name+"%'");
			while(rs.next()){
				Course cour = new Course(rs.getInt("id"),rs.getString("cname"),rs.getInt("period"));
				cours.add(cour);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return cours;
		}
		finally{
			try {
				Util.closeConnection(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cours;
	}
	public List <Course> findCourseById(int id){
		List <Course> cours = new ArrayList<Course>();
		Connection con = null;
		try {
			con = Util.getConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from db_course_p where id ="+id);
			while(rs.next()){
				Course cour = new Course(rs.getInt("id"),rs.getString("cname"),rs.getInt("period"));
				cours.add(cour);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return cours;
		}
		finally{
			try {
				Util.closeConnection(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cours;
	}
	public boolean insertCourse(Course cour){
		Connection con = null;
		try {
			con = Util.getConnection();
			String sql = "insert into db_course_p values (?,?,?)";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1, cour.getId());	
			pst.setString(2, cour.getCname());
			pst.setInt(3, cour.getPeriod());
			pst.execute();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		}
		finally{
			try {
				Util.closeConnection(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public boolean deleteCourseById(int id){
		Connection con = null;
		try {
			con = Util.getConnection();
			String sql = "delete from db_course_p where id=?";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1, id);	
			pst.execute();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		}
		finally{
			try {
				Util.closeConnection(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public boolean updateCourse(Course cour){
		Connection con = null;
		try {
			con = Util.getConnection();
			String sql = "update db_course_p set cname=?,period=? where id=?";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(3, cour.getId());	
			pst.setString(1, cour.getCname());
			pst.setInt(2, cour.getPeriod());
			pst.execute();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		}
		finally{
			try {
				Util.closeConnection(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
